package com.example.aacwebdemo.h2;

import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

/**
 * @author zhao.xin06679
 * @date 2021年06月22日 14:14
 */

@RestController
@RequestMapping("/h2")
public class H2Controller {

    @Autowired
    JdbcTemplate h2JdbcTemplate;

    @RequestMapping(value="h2CountTest",method = RequestMethod.GET)
    public String h2CountTest(){
        Object o = h2JdbcTemplate.queryForList("select * from h2test limit 10");
        Gson g = new Gson();
        return g.toJson(o);
    }

    @RequestMapping(value="h2RadmonTest",method = RequestMethod.GET)
    public String h2RadmonTest(){

        long start = System.currentTimeMillis();

        for(int i = 0 ; i < 100000; i++){
            Random r = new Random(i);
            long  l = r.nextInt(100000);
            //System.out.println("select * from h2test where account_id = " + l);
            Object o = h2JdbcTemplate.queryForList("select * from h2test where account_id = " + l );

        }

        long end = System.currentTimeMillis();

        return (end - start) + "";
    }

    @RequestMapping(value="saveh2Test",method = RequestMethod.GET)
    public String saveH2Test(){


        long start = System.currentTimeMillis();

        for (int i = 0 ; i < 100000; i++){
            String sql = "insert into h2test values ("+i+","+ i + ","+ i+",'1123，23，13，21，321，3123',1,"+i+")";

            h2JdbcTemplate.execute(sql);
        }

        long end = System.currentTimeMillis();
        return  (start - end) + "";
    }

}
